package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.Store;
import net.wanho.service.StoreService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/store")
@RequiredArgsConstructor
public class StoreController {
    private final StoreService storeService;

    /**
     *  所有仓库列表
     */
    @GetMapping("/store-list")
    public AjaxResult storeList() {
        return AjaxResult.success(storeService.list());
    }

    /**
     *  仓库分页列表
     * @param store
     * @param pageNum
     * @param pageSize
     */
    @GetMapping("/store-page-list")
    public AjaxResult storePageList(Store store,
                                    @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        return AjaxResult.success(storeService.pageList(store, pageNum, pageSize));
    }

    /**
     *  仓库编号校验
     */
    @GetMapping("/store-num-check")
    public AjaxResult storeNumCheck(String storeNum) {
        return AjaxResult.success(storeService.lambdaQuery().eq(Store::getStoreNum, storeNum).count() <= 0);
    }

    /**
     *  添加仓库
     */
    @PostMapping("/store-add")
    public AjaxResult storeAdd(@RequestBody Store store) {
        storeService.save(store);
        return AjaxResult.success(null, "仓库添加成功！");
    }

    /**
     *  修改仓库
     */
    @PutMapping("/store-update")
    public AjaxResult storeUpdate(@RequestBody Store store) {
        storeService.updateById(store);
        return AjaxResult.success(null, "仓库修改成功！");
    }

    /**
     *  删除仓库
     */
    @DeleteMapping("/store-delete/{id}")
    public AjaxResult storeDelete(@PathVariable Integer id) {
        storeService.removeById(id);
        return AjaxResult.success(null, "仓库删除成功！");
    }

    /**
     *  仓库导出
     */
    @GetMapping("/exportTable")
    public AjaxResult exportTable(Store store,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        return AjaxResult.success(storeService.pageList(store, pageNum, pageSize).getResultList());
    }
}
